Imports Microsoft.VisualBasic
Imports MB.PsychCal.DAL
Imports System.Collections.Generic

Namespace MB.PsychCal.BLL.Articles
    Public Class Article
        Inherits BizObject

        Public Sub New(ByVal id As Integer, ByVal madeby As String, ByVal subject As String, _
        ByVal message As String, ByVal addeddate As DateTime, ByVal isexpired As Boolean)

            Me.ID = id
            Me.madeby = madeby
            Me.subject = subject
            Me.message = message
            Me.AddedDate = addeddate
            Me.isexpired = isexpired
        End Sub

        Private _id As Integer = 0
        Private _madeby As String = ""
        Private _subject As String = ""
        Private _message As String = ""
        Private _addeddate As DateTime = DateTime.Now
        Private _isexpired As Boolean = False
        Public Property ID() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property
        Public Property madeby() As String
            Get
                Return _madeby
            End Get
            Set(ByVal value As String)
                _madeby = value
            End Set
        End Property

        Public Property subject() As String
            Get
                Return _subject
            End Get
            Set(ByVal value As String)
                _subject = value
            End Set
        End Property

        Public Property message() As String
            Get
                Return _message
            End Get
            Set(ByVal value As String)
                _message = value
            End Set
        End Property
        Public Property AddedDate() As DateTime
            Get
                Return _addeddate
            End Get
            Set(ByVal value As DateTime)
                _addeddate = value
            End Set
        End Property

        Public Property isexpired() As Boolean
            Get
                Return _isexpired
            End Get
            Set(ByVal value As Boolean)
                _isexpired = value
            End Set
        End Property

        ' Methods

        ' Returns collection of all articles
        Public Shared Function GetArticles() As List(Of Article)
            Return GetArticles(0, BizObject.MAXROWS)
        End Function

        Public Shared Function GetArticles( _
                ByVal startRowIndex As Integer, ByVal maximumRows As Integer) _
                As List(Of Article)

            Dim articles As List(Of Article) = Nothing
            Dim key As String = _
                "Articles_Articles_" + startRowIndex.ToString() + "_" + maximumRows.ToString()

            If Settings.EnableCaching AndAlso Not IsNothing(BizObject.Cache(key)) Then
                articles = CType(BizObject.Cache(key), List(Of Article))
            Else
                Dim recordset As List(Of ArticleDetails) = _
                    SiteProvider.Articles.GetArticles( _
                    GetPageIndex(startRowIndex, maximumRows), maximumRows)
                articles = GetArticleListFromArticleDetailsList(recordset)
                CacheData(key, articles)
            End If
            Return articles
        End Function


        Public Shared Function GetArticles(ByVal CategoryID As Integer, ByVal startRowIndex As Integer, ByVal maximumRows As Integer) As List(Of Article)
            If CategoryID <= 0 Then
                Return GetArticles(startRowIndex, maximumRows)
            End If

            Dim articles As List(Of Article) = Nothing
            Dim key As String = "Articles_Articles_" + CategoryID.ToString() + "_" + startRowIndex.ToString() + "_" + maximumRows.ToString()

            If Settings.EnableCaching AndAlso Not IsNothing(BizObject.Cache(key)) Then
                articles = CType(BizObject.Cache(key), List(Of Article))
            Else
                Dim recordset As List(Of ArticleDetails) = SiteProvider.Articles.GetArticles( _
                    GetPageIndex(startRowIndex, maximumRows), maximumRows)
                articles = GetArticleListFromArticleDetailsList(recordset)
                CacheData(key, articles)
            End If

            Return articles
        End Function

        Public Shared Function GetArticleCount() As Integer
            Dim articleCount As Integer = 0
            Dim key As String = "Articles_ArticleCount"

            If Settings.EnableCaching AndAlso Not IsNothing(BizObject.Cache(key)) Then
                articleCount = CInt(BizObject.Cache(key))
            Else
                articleCount = SiteProvider.Articles.GetArticleCount()
                CacheData(key, articleCount)
            End If

            Return articleCount
        End Function

        Public Shared Function GetArticles( _
                ByVal publishedOnly As Boolean, ByVal startRowIndex As Integer, _
                ByVal maximumRows As Integer) _
                As List(Of Article)

            If Not publishedOnly Then
                Return GetArticles(startRowIndex, maximumRows)
            End If

            Dim articles As List(Of Article) = Nothing
            Dim key As String = "Articles_Articles_" + publishedOnly.ToString() + "_" + startRowIndex.ToString() + "_" + maximumRows.ToString()

            If Settings.EnableCaching AndAlso Not IsNothing(BizObject.Cache(key)) Then
                articles = CType(BizObject.Cache(key), List(Of Article))
            Else
                '   Dim recordset As List(Of ArticleDetails) = _
                'SiteProvider.Articles.GetPublishedArticles(DateTime.Now, _
                'GetPageIndex(startRowIndex, maximumRows), maximumRows)
                'articles = GetArticleListFromArticleDetailsList(recordset)
                'CacheData(key, articles)
            End If
            Return articles
        End Function

        Public Shared Function GetArticles( _
            ByVal publishedOnly As Boolean, ByVal categoryID As Integer) _
            As List(Of Article)

            Return GetArticles(publishedOnly, categoryID, 0, BizObject.MAXROWS)
        End Function

        Public Shared Function GetArticles( _
                ByVal publishedOnly As Boolean, ByVal categoryID As Integer, _
                ByVal startRowIndex As Integer, ByVal maximumRows As Integer) _
                As List(Of Article)

            If Not publishedOnly Then
                Return GetArticles(categoryID, startRowIndex, maximumRows)
            End If

            If categoryID <= 0 Then
                Return GetArticles(publishedOnly, startRowIndex, maximumRows)
            End If

            Dim articles As List(Of Article) = Nothing
            Dim key As String = "Articles_Articles_" + publishedOnly.ToString() + _
                "_" + categoryID.ToString() + "_" + startRowIndex.ToString() + _
                "_" + maximumRows.ToString()

            If Settings.EnableCaching AndAlso Not IsNothing(BizObject.Cache(key)) Then
                articles = CType(BizObject.Cache(key), List(Of Article))
                'Else
                'Dim recordset As List(Of ArticleDetails) = _
                'SiteProvider.Articles.GetPublishedArticles( _
                'categoryID, DateTime.Now, GetPageIndex(startRowIndex, maximumRows), maximumRows)
                'articles = GetArticleListFromArticleDetailsList(recordset)
                'CacheData(key, articles)
            End If
            Return articles
        End Function



        Public Shared Function GetArticleByID(ByVal articleID As Integer) As Article
            Dim m_article As Article = Nothing
            Dim key As String = "Articles_Article_" + articleID.ToString()

            If Settings.EnableCaching AndAlso Not IsNothing(BizObject.Cache(key)) Then
                m_article = CType(BizObject.Cache(key), Article)
            Else
                m_article = GetArticleFromArticleDetails(SiteProvider.Articles.GetArticleByID(articleID))
                CacheData(key, m_article)
            End If
            Return m_article
        End Function

        Public Shared Function InsertArticle(ByVal subject As String, _
                ByVal message As String) _
                As Integer

            ' make sure approved option is false if user is not an admin or editor


            subject = BizObject.ConvertNullToEmptyString(subject)
            message = BizObject.ConvertNullToEmptyString(message)

            'If addeddate = DateTime.MinValue Then
            'AddedDate = DateTime.Now
            'End If

            Dim record As New ArticleDetails(300, BizObject.CurrentUserName, _
               subject, message, DateTime.Now, 0)
            Dim ret As Integer = SiteProvider.Articles.InsertArticle(record)

            BizObject.PurgeCacheItems("articles_article")
            Return ret
        End Function

        'Public Shared Function IncrementArticleViewCount(ByVal id As Integer) As Boolean
        '   Return SiteProvider.Articles.IncrementArticleViewCount(id)
        'End Function

        Public Shared Function GetArticleFromArticleDetails( _
                ByVal record As ArticleDetails) _
                As Article

            If IsNothing(record) Then
                Return Nothing
            Else
                Return New Article(record.ID, record.madeby, _
                   record.subject, record.message, record.addeddate, _
                   record.isexpired)
            End If
        End Function

        Public Shared Function GetArticleListFromArticleDetailsList( _
                ByVal recordset As List(Of ArticleDetails)) _
                As List(Of Article)

            Dim articles As New List(Of Article)
            For Each record As ArticleDetails In recordset
                articles.Add(GetArticleFromArticleDetails(record))
            Next
            Return articles
        End Function
        Protected Shared ReadOnly Property Settings() As PsychCalConfigSection.ArticlesElement 'pg 229 this object is in the App_Code/ConfigSection.vb
            Get
                Return Globals.Settings.Articles
            End Get
        End Property

        Protected Shared Sub CacheData(ByVal key As String, ByVal data As Object)
            If Settings.EnableCaching AndAlso Not IsNothing(data) Then
                BizObject.Cache.Insert(key, data, Nothing, _
                    DateTime.Now.AddSeconds(Settings.CacheDuration), TimeSpan.Zero)
            End If
        End Sub
    End Class
End Namespace
